#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Any

from cta.interface.action.prepare_data.abstract_prepare_data_action import AbstractPrepareDataAction
from web.domain.responsibility_chain_dto import ResponsibilityChainDto
from web.models.commodity_future_date_contract_data import CommodityFutureDateContractData
from web.models.quant2_commodity_future_filter import Quant2CommodityFutureFilter
from web.manager.log_manager import LogManager

Logger = LogManager.get_logger(__name__)

class CommonPrepareDataAction(AbstractPrepareDataAction):
    """
    普通的准备数据动作
    """

    def exec(self, responsibility_chain_dto: ResponsibilityChainDto = None) -> Any:
        Logger.info("准备数据：清空quant2_c_f_filter表，向quant2_c_f_filter表中插入记录")

        transaction_date: str = responsibility_chain_dto.transaction_date

        # 清空quant2_c_f_filter表
        self.quant2_commodity_future_filter_dao.truncate_table()

        # 向quant2_c_f_filter表中插入记录
        filter_dict = {'transaction_date': transaction_date}
        commodity_future_date_contract_data_list: list[CommodityFutureDateContractData] = self.commodity_future_date_contract_data_dao.find_list(filter_dict, dict(), list())
        # commodity_future_date_contract_data_list: list[CommodityFutureDateContractData] = CommodityFutureDateContractData.objects.filter(transaction_date=transaction_date).filter(name__startswith='花生')
        if commodity_future_date_contract_data_list is not None and len(commodity_future_date_contract_data_list) > 0:
            quant2_commodity_future_filter_list: list[Quant2CommodityFutureFilter] = list()
            for commodity_future_date_contract_data in commodity_future_date_contract_data_list:
                quant2_commodity_future_filter: Quant2CommodityFutureFilter = Quant2CommodityFutureFilter()
                quant2_commodity_future_filter.code = commodity_future_date_contract_data.code
                quant2_commodity_future_filter.turnover = commodity_future_date_contract_data.turnover
                quant2_commodity_future_filter_list.append(quant2_commodity_future_filter)
            self.quant2_commodity_future_filter_dao.save_batch(quant2_commodity_future_filter_list)
        else:
            Logger.warning("日期[%s]没有交易记录", transaction_date)

        super().exec(responsibility_chain_dto)